package com.das.mechanic_alone.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.umeng.analytics.MobclickAgent;
import com.zyyoona7.wheel.WheelView;

import java.util.List;

/**
 * Created by chensi on 2018/3/20.
 */

public class X3NumberPrecentPickerPopWindow implements PopupWindow.OnDismissListener, View.OnClickListener {

    private Activity mContext;
    private PopupWindow popupWindow;
    private View view;

    private final WheelView<String> wv_view;

    private String startData = "0";

    private String selectData = "0";

    private int startIndex = 0;

    private TextView tv_num_picker;

    public X3NumberPrecentPickerPopWindow(Activity mContext, String startData, List<String> dataList, String title) {
        this.mContext = mContext;
        this.startData = startData;
        //设置PopupWindow的View
        view = LayoutInflater.from(mContext).inflate(R.layout.x3_number_percent_picker, new LinearLayout(mContext),false);
        wv_view = view.findViewById(R.id.wv_view);
        tv_num_picker = view.findViewById(R.id.tv_num_picker);
        tv_num_picker.setText(title);
        for (int i = 0; i < dataList.size(); i++) {
            if (dataList.get(i).equals(startData)) {
                startIndex = i;
            }
        }
        wv_view.setData(dataList);
        wv_view.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_view.setSelectedItemPosition(startIndex);
        wv_view.setSoundEffectResource(R.raw.x3_button_choose);
        wv_view.setSoundEffect(true);
        wv_view.setPlayVolume(0.5f);
        wv_view.setOnItemSelectedListener((wheelView, data, position) -> selectData = data);
    }

    public void showPopupWindow (View v) {
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        MobclickAgent.onPageStart("检测系统进油气量弹窗");

        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(null);
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        popupWindow.setAnimationStyle(R.style.PopupWindow1);
        //设置位置
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //setBackgroundAlpha(0.9f);
        //设置PopupWindow的View点击事件
        setOnPopupViewClick(view);

    }

    private void setOnPopupViewClick(View view) {
        Button btn_cancel = view.findViewById(R.id.btn_cancel);
        Button btn_affirm = view.findViewById(R.id.btn_affirm);

        btn_affirm.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = alpha;
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        mContext.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_affirm) {
            if (popupWindow != null) {
                popupWindow.dismiss();
            }
            if (iOnItemCameraClick != null) {
                iOnItemCameraClick.iOpenAffirmListener(selectData);
            }
        } else if (id == R.id.btn_cancel) {
            if (popupWindow != null) {
                popupWindow.dismiss();
            }
            if (iOnItemCameraClick != null) {
                iOnItemCameraClick.iOpenCancelListener();
            }
        }
    }

    IOpenNumberClick iOnItemCameraClick;

    public void setiOnItemCameraClick(IOpenNumberClick iOnItemCameraClick) {
        this.iOnItemCameraClick = iOnItemCameraClick;
    }

    @Override
    public void onDismiss() {
        MobclickAgent.onPageStart("检测系统进油气量弹窗");
        if (iOnItemCameraClick != null) {
            iOnItemCameraClick.iOnShowView(false);
        }
    }

    public interface IOpenNumberClick {

        void iOpenCancelListener();

        void iOpenAffirmListener(String selectData);

        void iOnShowView(boolean isShow);
    }

}
